﻿using MasterMasterMode.Content.NPCs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace MasterMasterMode.Common.GlobalItems
{
    public class RecallBlockItem : GlobalItem
    {
        public static bool MagicalHarpActive => CheckForRecallBlock();
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void PostDrawInInventory(Item item, SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale)
        {
            if (IsRecallItem(item) && MagicalHarpActive)
            {
                DrawXMark(position, scale);
            }
            base.PostDrawInInventory(item, spriteBatch, position, frame, drawColor, itemColor, origin, scale);
        }
        private static void DrawXMark(Vector2 position, float scale)
        {
            Texture2D xTexture = ModContent.Request<Texture2D>("MasterMasterMode/Assets/Textures/Items/CoolDown").Value;
            Vector2 drawPosition = position + new Vector2(10f, 10f) * scale;
            Main.spriteBatch.Draw(xTexture, drawPosition, null, Color.White, 0f, xTexture.Size() / 2f, scale * 0.8f, SpriteEffects.None, 0f);
        }
        private static bool IsRecallItem(Item item)
        {
            return item.type == ItemID.RecallPotion ||
                   item.type == ItemID.TeleportationPotion ||
                   item.type == ItemID.MagicMirror ||
                   item.type == ItemID.IceMirror ||
                   item.type == ItemID.CellPhone ||
                   item.type == ItemID.Shellphone ||
                   item.type == ItemID.ShellphoneHell ||
                   item.type == ItemID.ShellphoneOcean;
        }
        private static bool CheckForRecallBlock()
        {
            foreach (var npc in Main.npc)
            {
                if (npc.active && npc.type == ModContent.NPCType<MagicalHarp>())
                {
                    if (npc.ModNPC is MagicalHarp harp && harp.currentState != MagicalHarp.AIState.Idle)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
